package com.bw.mvvm_common.weidget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.blankj.utilcode.util.ConvertUtils;
import com.bw.mvvm_common.R;


public
/**
 * @author Guan
 * @description:
 * @date :2021/9/9 0009 下午 6:46
 *
 */
class BottomButton extends RelativeLayout implements View.OnClickListener {
    private LinearLayout linearLayout;
    private ImageView img;
    private TextView contentTv;
    private BottomSelectListenner listenner;
    private boolean setFlag =false;
    private int namesize = ConvertUtils.sp2px(12);
    private int selsize = ConvertUtils.sp2px(15);
    private int nomalColor = Color.BLACK;
    private int selColor = Color.RED;
    private int nomalImg = 0 ;
    private int selImg = 0;
    private String contact ="";
    //设置要显示的文字方法

    public BottomButton setContact(String contact) {
        this.contact = contact;
        return this;
    }

    // 设置选中图片

    public BottomButton setSelImg(int selImg) {
        this.selImg = selImg;
        return this;
    }

    //设置未选中图片

    public BottomButton setNomalImg(int nomalImg) {
        this.nomalImg = nomalImg;
        return this;
    }

    // 设置选中文字颜色

    public BottomButton setSelColor(int selColor) {
        this.selColor = selColor;
        return this;
    }

    // 设置未选中文字颜色

    public BottomButton setNomalColor(int nomalColor) {
        this.nomalColor = nomalColor;
        return this;
    }

    //设置选中字体大小

    public BottomButton setSelsize(int selsize) {
        this.selsize = ConvertUtils.sp2px(selsize);
        return this;
    }

    //设置未选中字体大小

    public BottomButton setNamesize(int namesize) {
        this.namesize = ConvertUtils.sp2px(namesize);
        return this;
    }

    // 点击回调接口
    public BottomButton setListenner(BottomSelectListenner listenner) {
        this.listenner = listenner;
        return this;
    }
    //设置是否选中


    public BottomButton setSetFlag(boolean setFlag) {
        this.setFlag = setFlag;
        refresh();
        return this;
    }

    public BottomButton(Context context, AttributeSet attrs) {
        super(context, attrs);
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.bottom_arrs);
        setFlag = typedArray.getBoolean(R.styleable.bottom_arrs_selflag,false);
        typedArray.recycle();
        init();
    }

    private void init(){
        linearLayout = new LinearLayout(getContext());
        LayoutParams linearLp = new LayoutParams(
                LayoutParams.WRAP_CONTENT,
                LayoutParams.WRAP_CONTENT);
        linearLp.addRule(RelativeLayout.CENTER_IN_PARENT);
        linearLayout.setLayoutParams(linearLp);
        linearLayout.setOrientation(LinearLayout.VERTICAL);
        linearLayout.setGravity(Gravity.CENTER_HORIZONTAL);
        img = new ImageView(getContext());
        LinearLayout.LayoutParams imglp = new LinearLayout.LayoutParams(
            ConvertUtils.dp2px(25),
            ConvertUtils.dp2px(35)
        );
        img.setLayoutParams(imglp);
        linearLayout.addView(img);
        contentTv = new TextView(getContext());
        LinearLayout.LayoutParams contentLp = new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.WRAP_CONTENT,
                LinearLayout.LayoutParams.WRAP_CONTENT);
        contentTv.setLayoutParams(contentLp);
        linearLayout.addView(contentTv);
        addView(linearLayout);
        setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        setFlag = true;
        refresh();
        if (listenner!=null){
            listenner.onSelect(getId());
        }

    }

    public void refresh(){
        int imgres = nomalImg;
        int tvColor = nomalColor;
        int tvSize = namesize;
        if (setFlag){
            imgres = selImg;
            tvColor = selColor;
            tvSize = selsize;
        }
        img.setImageResource(imgres);
        contentTv.setTextColor(tvColor);
        contentTv.setTextSize(tvSize);
        contentTv.setText(contact);
    }


    public interface BottomSelectListenner{
        void onSelect(int id);
    }
}
